package com.pansoft.openplanet.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.utils.ResStringUtil;
import com.pansoft.newasset.fragment.NewAssetCardsFragment;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.BarcodeReceiveActivity;
import com.pansoft.openplanet.activity.NewIPAssetsWalletDetailsActivity;
import com.pansoft.openplanet.activity.NewTransferActivity;
import com.pansoft.openplanet.activity.TradeLineActivity;
import com.pansoft.openplanet.bean.CardBean;
import com.pansoft.openplanet.bean.CardWalletInfo;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.util.TCStringUtil;
import com.utilcode.util.ToastUtils;

import java.util.List;

/**
 * 卡包详情列表适配器
 *
 * @author maming
 * @date 2019/4/19 9:12
 */
public class AssetCardsDetailsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_BODY = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    private Context mContext;
    private CardBean cardBean;
    private List<CardWalletInfo> cardWalletInfoList;
    //
    private OnEyeShowNumClickListener onEyeShowNumClickListener;

    public AssetCardsDetailsListAdapter(Context mContext, CardBean cardBean) {
        this.mContext = mContext;
        this.cardBean = cardBean;
        this.cardWalletInfoList = cardBean.getWalletInfoList();
    }

    public void setOnEyeShowNumClickListener(OnEyeShowNumClickListener onEyeShowNumClickListener) {
        this.onEyeShowNumClickListener = onEyeShowNumClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_HEADER) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_cards_details_header, parent, false);
            return new CardsHeaderHolder(view);
        } else if (viewType == VIEW_TYPE_BODY) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_cards_details, parent, false);
            return new CardsAssetHolder(view);
        } else if (viewType == VIEW_TYPE_FOOTER) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_cards_details_footer, parent, false);
            return new CardsFooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //头布局
        if (holder instanceof CardsHeaderHolder) {
            ((CardsHeaderHolder) holder).iv_background.setImageDrawable(NewAssetCardsFragment
                    .getCardTypeBackgroundDrawable(mContext, cardBean.getCardType()));
            ((CardsHeaderHolder) holder).tv_card_number.setText(cardBean.getUid());
            ((CardsHeaderHolder) holder).tv_card_number.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    copyAddress(cardBean.getUid());
                }
            });
            ((CardsHeaderHolder) holder).tv_card_type.setText(CardBean.getCardTypeName(cardBean.getCardType()));
            ((CardsHeaderHolder) holder).tv_card_amount.setText(String.valueOf(cardBean.getWalletInfoList().size()));
            if (cardBean.isShowNum()) {
                ((CardsHeaderHolder) holder).iv_card_eye.setImageResource(R.drawable.asset_ic_show_eye);
                ((CardsHeaderHolder) holder).tv_card_number.setText(TCStringUtil.formatCardNoShow(cardBean.getUid()));
            } else {
                ((CardsHeaderHolder) holder).iv_card_eye.setImageResource(R.drawable.asset_ic_hide_eye);
                ((CardsHeaderHolder) holder).tv_card_number.setText(TCStringUtil.formatCardNoHide(cardBean.getUid()));
            }
            //点击眼睛按钮隐藏或者显示卡号
            ((CardsHeaderHolder) holder).iv_card_eye.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onEyeShowNumClickListener.isShowNmu();
                }
            });
            return;
        }
        //脚布局
        if (holder instanceof CardsFooterHolder) {
            ((CardsFooterHolder) holder).tv_noMore_content.setText(R.string.open_planet_no_content);
            return;
        }
        //中间布局
        final CardWalletInfo cardWalletInfo = cardWalletInfoList.get(position - 1);
        LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext,((CardsAssetHolder) holder).iv_wallet,cardWalletInfo.getWalletAvatar(),0,R.drawable.star_asset_default_wallet_icon,10);
        String walletName = cardWalletInfo.getWalletName();
        if (walletName == null || "".equals(walletName)) {
            walletName = ResStringUtil.getString(R.string.wallet) + (position + 1);
        }
        ((CardsAssetHolder) holder).tv_account_name.setText(walletName);
        ((CardsAssetHolder) holder).tv_wallet_address.setText(cardWalletInfo.getAddress());
//        ((CardsAssetHolder) holder).tv_wallet_type.setText(CardBean.getCardTypeName(cardBean.getCardType()));
        String address = cardWalletInfo.getAddress();
        if (address.length() > 20) {
            address = address.replace(address.substring(6, address.length() - 4), "****");
        } else {
        }
        ((CardsAssetHolder) holder).tv_wallet_address.setText(address);
//        ((CardsAssetHolder) holder).tv_wallet_type.setText("普卡");
        //
        if ("true".equals(EnvironmentVariable.getProperty("cards_show_num", "true"))) {
            ((CardsAssetHolder) holder).tv_assets.setText(cardWalletInfo.getWalletBalance());
        } else {
            ((CardsAssetHolder) holder).tv_assets.setText("******");
        }
        if (cardWalletInfo.getChainId() == IPAssetsConstant.CHAIN_ID_ETH) {
            ((CardsAssetHolder) holder).tv_wallet_type.setText("ETH");
            ((CardsAssetHolder) holder).iv_coin.setImageResource(R.drawable.eth);
        } else {
            ((CardsAssetHolder) holder).tv_wallet_type.setText("BTC");
            ((CardsAssetHolder) holder).iv_coin.setImageResource(R.drawable.btc_copy);
        }
        //更新钱包地址或者头像
        ((CardsAssetHolder) holder).tv_account_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewIPAssetsWalletDetailsActivity.start(mContext, cardBean.getUid(), cardWalletInfo.getAddress());
            }
        });
        ((CardsAssetHolder) holder).iv_wallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewIPAssetsWalletDetailsActivity.start(mContext, cardBean.getUid(), cardWalletInfo.getAddress());
            }
        });
        //点击复制按钮
        ((CardsAssetHolder) holder).iv_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                copyAddress(cardWalletInfo.getAddress());
            }
        });
        //点击转入按钮
        ((CardsAssetHolder) holder).tv_trade_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BarcodeReceiveActivity.start(mContext, cardWalletInfo.getWalletName(), cardWalletInfo.getAddress(), "",
                        cardWalletInfo.getChainId() == IPAssetsConstant.CHAIN_ID_ETH ? "ETH" : "BTC", "", cardWalletInfo.getChainId(), "");
            }
        });
        //点击明细按钮
        ((CardsAssetHolder) holder).tv_trade_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //进入交易明细界面
                TradeLineActivity.startTradeLineActivity
                        (mContext, cardWalletInfo.getChainId(), cardWalletInfo.getWalletName(), cardWalletInfo.getAddress(), "", cardWalletInfo.getWalletBalance(),
                                null, null, 2);
            }
        });
        //点击转出按钮
        ((CardsAssetHolder) holder).tv_trade_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //普通转账，非Token代币转账
                NewTransferActivity.start(mContext, cardWalletInfo.getWalletName(), cardWalletInfo.getChainId(), cardWalletInfo.getAddress(), cardWalletInfo.getWalletBalance(),
                        null, 2);
            }
        });
    }

    /**
     * 复制地址
     *
     * @param address 地址
     */
    private void copyAddress(String address) {
        //复制账号
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(address);
        ToastUtils.setBgColor(mContext.getResources().getColor(R.color.black));
        ToastUtils.setMsgColor(mContext.getResources().getColor(R.color.op_text_white_color));
        ToastUtils.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 150);
        ToastUtils.showShort(ResStringUtil.getString(R.string.open_planet_asset_child_wallet_list_copied_clipboard));
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_HEADER;
        } else if (position == cardBean.getWalletInfoList().size() + 1) {
            return VIEW_TYPE_FOOTER;
        } else {
            return VIEW_TYPE_BODY;
        }
    }

    @Override
    public int getItemCount() {
        return cardBean.getWalletInfoList().size() + 2;
    }

    class CardsHeaderHolder extends RecyclerView.ViewHolder {
        ImageView iv_background;
        TextView tv_card_number;
        TextView tv_card_type;
        TextView tv_card_amount;
        ImageView iv_card_eye;

        public CardsHeaderHolder(View itemView) {
            super(itemView);
            iv_background = itemView.findViewById(R.id.iv_bg);
            tv_card_number = itemView.findViewById(R.id.tv_card_number);
            tv_card_type = itemView.findViewById(R.id.tv_card_type);
            tv_card_amount = itemView.findViewById(R.id.tv_card_amount);
            iv_card_eye = itemView.findViewById(R.id.iv_card_eye);
        }
    }

    class CardsAssetHolder extends RecyclerView.ViewHolder {
        ImageView iv_wallet;
        TextView tv_account_name;
        TextView tv_wallet_address;
        TextView tv_assets;
        TextView tv_wallet_type;
        ImageView iv_copy;
        ImageView iv_coin;
        TextView tv_trade_in;
        TextView tv_trade_list;
        TextView tv_trade_out;

        public CardsAssetHolder(View itemView) {
            super(itemView);
            iv_wallet = itemView.findViewById(R.id.iv_wallet);
            tv_account_name = itemView.findViewById(R.id.tv_account_name);
            tv_wallet_address = itemView.findViewById(R.id.tv_wallet_address);
            tv_assets = itemView.findViewById(R.id.tv_assets);
            tv_wallet_type = itemView.findViewById(R.id.tv_wallet_type);
            iv_copy = itemView.findViewById(R.id.iv_copy);
            iv_coin = itemView.findViewById(R.id.iv_coin);
            tv_trade_in = itemView.findViewById(R.id.tv_trade_in);
            tv_trade_list = itemView.findViewById(R.id.tv_trade_list);
            tv_trade_out = itemView.findViewById(R.id.tv_trade_out);
        }
    }

    class CardsFooterHolder extends RecyclerView.ViewHolder {

        TextView tv_noMore_content;

        public CardsFooterHolder(View itemView) {
            super(itemView);
            tv_noMore_content = itemView.findViewById(R.id.tv_noMore_content);
        }
    }

    /**
     * 点击眼睛按钮是否展示数量
     */
    public interface OnEyeShowNumClickListener {
        void isShowNmu();
    }
}
